/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.samlss.lighter.interceptor;

import java.math.BigDecimal;

/**
 * 回弹差值器
 *
 * @author: zhu_zhonglin
 * @since: 2021/4/25
 */
public class BounceInterpolator implements BaseInterpolator {
    @Override
    public float getInterpolation(float input) {
        BigDecimal inputDecimal = new BigDecimal(input);
        inputDecimal = inputDecimal.multiply(new BigDecimal("1.1226"));
        if (inputDecimal.floatValue() < 0.3535f) {
            return bounce(inputDecimal).floatValue();
        } else if (inputDecimal.floatValue() < 0.7408f) {
            return bounce(inputDecimal.subtract(new BigDecimal("0.54719"))).add(new BigDecimal("0.7")).floatValue();
        } else if (inputDecimal.floatValue() < 0.9644f) {
            return bounce(inputDecimal.subtract(new BigDecimal("0.8526"))).add(new BigDecimal("0.9")).floatValue();
        } else {
            return bounce(inputDecimal.subtract(new BigDecimal("1.0435"))).add(new BigDecimal("0.95")).floatValue();
        }
    }

    private static BigDecimal bounce(BigDecimal input) {
        return input.multiply(input).multiply(new BigDecimal("8.0"));
    }

}
